//
//  BPTransmitView.swift
//  BPWeiBo
//
//  Created by 扒皮大叔 on 16/4/12.
//  Copyright © 2016年 apple. All rights reserved.f
import UIKit
import SnapKit
import YYText
import SVProgressHUD
class BPTransmitView: UIView {
    
    //懒加载图片视图
    lazy var retweenPictureView:BPHomePictureView = BPHomePictureView()
    //定义记录的约束,
    var bottomPicture:Constraint?

    
    var statusCellViewModel:BPStatusCellViewModel? {
        didSet{
            
            //let tempText = statusCellViewModel?.statusListModel?.retweeted_status?.text ?? ""
            //设置属性文本
            let tempText = statusCellViewModel?.zhuangfaAtt
            textLabel.attributedText = tempText
            
            //在这里判断原创微博的图片视图是否存在,根据它是否存在来改变原创微博底部的约束和图片视图的约束
            //更新约束之前先把原来的约束卸载掉
            self.bottomPicture?.uninstall()
            if let urls = statusCellViewModel?.statusListModel?.retweeted_status?.pic_urls where urls.count > 0  {
                //让图片视图隐藏
                retweenPictureView.hidden = false
                //让图片视图的数据源为空
                retweenPictureView.pictureViewImg = urls
                //更新视图的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomPicture = make.bottom.equalTo(retweenPictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            }else {
                //让图片视图显示
                retweenPictureView.hidden = true
                //设置图片视图的数据源
                retweenPictureView.pictureViewImg = nil
                //更新视图的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomPicture = make.bottom.equalTo(textLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 0.9, alpha:1)
        //设置UI控件
        setUpUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图和约束
    private func setUpUI(){
        //添加视图
        addSubview(textLabel)
        
        addSubview(retweenPictureView)
        
        //设置约束
        textLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self.snp_top).offset(StatusCellMargin)
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            //make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        //MARK:设置图片视图的约束
        retweenPictureView.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(textLabel.snp_left)
            //make.right.equalTo(self).offset(-StatusCellMargin)
            //make.height.width.equalTo(100)
        })
        
        //设置原创微博的底部相对微博正文的底部约束
        self.snp_makeConstraints { (make) -> Void in
            //在设置约束的时候记录一下
            self.bottomPicture =  make.bottom.equalTo(retweenPictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        //处理点击事件  如果点击的是url  就跳转页面
        textLabel.highlightTapAction = { (containerView, text, range, rect) -> () in
            //将属性文本转换为字符串
            let str = text.string
            //截取子串 获取对应点击的字符串
            let subStr = (str as NSString).substringWithRange(range)
            SVProgressHUD.showInfoWithStatus(subStr)
            if subStr.containsString("http") {
                //是url 跳转页面  视图不能够跳转页面
                let web = ViewController()
                web.urlString = subStr
                //跳转页面
                self.navController()?.pushViewController(web, animated: true)
            }
        }
        

    }
    
    //懒加载控件
    //文字内容
    //private lazy var textLabel:UILabel = UILabel(text: "哈哈哈,我就是传说中的转发微博", textColor: UIColor.blackColor(), fontSize: 14, margin: StatusCellMargin)
    //MARK:利用YY的label来存放微博的征文
    private lazy var textLabel:YYLabel = {
        let yy = YYLabel()
        //设置最大的布局宽度
        yy.preferredMaxLayoutWidth = ScreenWidth - 2*StatusCellMargin
        //设置行数为自动换行
        yy.numberOfLines = 0
        
        return yy
    }()
    //图片内容
    private lazy var pictureView:UIView = UIView()
    //转发的名字
    private lazy var nameStr:UILabel = {
    let str = UILabel()
        str.textColor = UIColor.blueColor()
        str.font = UIFont.systemFontOfSize(12)
        str.text = "@周扒皮"
        return str
    }()
}
